
Public Class TransaksiObat
    Inherits templateFormDialog
    Private tambah As Boolean
    Public Property isTambah() As Boolean
        Get
            Return tambah
        End Get
        Set(ByVal value As Boolean)
            tambah = value
        End Set
    End Property

    Function validasi(ByVal retmsg As String) As Boolean
        Dim retval As Boolean = True
        Dim strdata As String = ""
        strdata = Utils.GridValueToText(GridTransaksi)
        If strdata = "" Then
            retmsg = "Data Entri Belum Dilengkapi"
            retval = False
            Exit Function
        End If
        Return retval
    End Function

    Public Overrides Sub SetSimpan()
        MyBase.SetSimpan()
        Dim msg As String = ""
        If Not validasi(msg) Then
            MsgBox(msg, MsgBoxStyle.Exclamation)
            Exit Sub
        End If
        Dim faktorkali As Integer
        If isTambah Then
            faktorkali = 1
        Else
            faktorkali = -1
        End If

        Dim strdata As String = ""
        strdata = Utils.GridValueToText(GridTransaksi)
        If Not strdata = "" Then
            getnotrx()
            Dim params As String()
            If Not strdata = "" Then
                params = strdata.Split("|")
                For int As Integer = 0 To params.Length - 1
                    Dim parami As String()
                    Dim parlist As New List(Of String)

                    parami = params(int).Split(",")
                    parlist.AddRange(parami)
                    parlist.Insert(0, txtnopo)
                    parlist.Insert(parlist.Count - 1, faktorkali)
                    parami = parlist.ToArray
                    params(int) = Join(parami, ",")
                Next
                strdata = String.Join("|", params)
                If strdata.EndsWith("|") Then
                    strdata = LSet(strdata, strdata.Length - 1)
                End If
                Dim dtretval As New DataTable

                If Utils.executeSP("proc_histoobat_exec", New Object() {txtnopo, GridTransaksi.RowCount - 1, GridTransaksi.ColumnCount + 2, strdata}, dtretval) Then
                    MsgBox(dtretval(0)(0))
                    GridTransaksi.Rows.Clear()
                    SetReset()
                End If


            End If
        End If

        
    End Sub

    Private Sub GridTransaksi_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles GridTransaksi.EditingControlShowing
        If GridTransaksi.CurrentCell.ColumnIndex = 2 Then
            Dim TextBox1 As TextBox
            TextBox1 = CType(e.Control, TextBox)
            RemoveHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox1_KeyPress)
            AddHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
        ElseIf GridTransaksi.CurrentCell.ColumnIndex = 0 Or GridTransaksi.CurrentCell.ColumnIndex = 4 Then
            Dim TextBox1 As TextBox
            TextBox1 = CType(e.Control, TextBox)
            RemoveHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            AddHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox1_KeyPress)
        End If
    End Sub
    Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = Utils.HanyaAngka(Asc(e.KeyChar))
    End Sub
    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

    End Sub

    Private Sub GridTransaksi_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridTransaksi.KeyDown
        If e.KeyValue = Keys.Delete Then
            Try
                Dim dgr As New DataGridViewRow
                dgr = GridTransaksi.CurrentRow
                GridTransaksi.Rows.Remove(dgr)
            Catch ex As Exception

            End Try
        End If
    End Sub
    Private Function validasirowgrid(ByVal dgr As DataGridView) As Boolean
        Dim retval As Boolean = True
        If GridTransaksi.RowCount > 0 Then
            If GridTransaksi.RowCount = 1 And GridTransaksi.Rows(0).IsNewRow Then
                retval = False
            End If
        Else
            retval = False
        End If
        Return retval


    End Function
    Private Sub GridTransaksi_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridTransaksi.KeyUp
        Dim ic, ir As Integer
        If validasirowgrid(GridTransaksi) Then
            ic = GridTransaksi.CurrentCell.ColumnIndex
            ir = GridTransaksi.CurrentCell.RowIndex
        Else
            GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.CurrentCell.RowIndex)
            Exit Sub
        End If

        If e.KeyValue = 13 Then
            If GridTransaksi.CurrentCell.ColumnIndex = 1 Then
                If Not IsDBNull(GridTransaksi(0, ir).Value) Or Not IsDBNull(GridTransaksi.CurrentCell.Value) Then
                    If Not GridTransaksi(0, ir).Value = "" Or Not GridTransaksi(1, ir).Value = "" Then



                        If Not IsDBNull(GridTransaksi(2, ir).Value) Then
                            If GridTransaksi(2, ir).Value = 0 Then
                                GridTransaksi(2, ir).Value = 1
                                'GridTransaksi(5, ir).Value = GridTransaksi(3, ir).Value * GridTransaksi(2, ir).Value

                                GridTransaksi.CurrentCell = GridTransaksi(2, ir)
                                GridTransaksi.CurrentCell.Selected = True
                            ElseIf GridTransaksi(2, ir).Value > 0 Then

                                GridTransaksi.CurrentCell = GridTransaksi(2, ir)
                                GridTransaksi.CurrentCell.Selected = True
                            End If
                        Else
                            GridTransaksi.CurrentCell = GridTransaksi(2, ir)
                            GridTransaksi.CurrentCell.Selected = True
                        End If

                    Else
                        GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                        GridTransaksi.CurrentCell.Selected = True
                    End If
                ElseIf GridTransaksi(0, ir).Value Is Nothing Then
                    GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                    GridTransaksi.CurrentCell.Selected = True
                End If

                'Dim eKey As New KeyEventArgs(Keys.Return)
                'GridTransaksi_KeyUp(sender, eKey)
            ElseIf GridTransaksi.CurrentCell.ColumnIndex = 3 Then
                GridTransaksi.CurrentCell = GridTransaksi(4, ir)
                GridTransaksi.CurrentCell.Selected = True
            ElseIf GridTransaksi.CurrentCell.ColumnIndex = 4 Then
                GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                GridTransaksi.CurrentCell.Selected = True
            End If
        End If
    End Sub
    Private Function GetAllEqual(ByVal tblname As String, ByVal finder As String, ByVal finderkey As String) As DataTable
        Dim dt As New DataTable
        Dim params As Object()
        Params = New Object() {"*", tblname, finder, finderkey}
        If Utils.executeSP("proc_searchallEqual", Params, dt) Then

        End If
        Return dt
    End Function
    Private Sub GridTransaksi_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridTransaksi.CellEndEdit
        If GridTransaksi.CurrentCell.ColumnIndex = 0 Then
            If Not IsDBNull(GridTransaksi.CurrentCell) Then
                If Not GridTransaksi.CurrentCell.Value = "" Then
                    'blm kelar=? masukkan search equal
                    Dim dt As New DataTable
                    dt = GetAllEqual("dataobat", "idobat", GridTransaksi.CurrentCell.Value)
                    If Utils.RowExist(dt) Then
                        GridTransaksi(1, e.RowIndex).Value = IIf(IsDBNull(dt(0)("namaobat")), "", dt(0)("namaobat"))
                        GridTransaksi(3, e.RowIndex).Value = IIf(IsDBNull(dt(0)("satuan")), "", dt(0)("satuan"))
                        'GridTransaksi(5, e.RowIndex).Value = GridTransaksi(2, e.RowIndex).Value * GridTransaksi(3, e.RowIndex).Value
                    
                    Else
                        MsgBox("data tidak terdaftar")
                        GridTransaksi.Rows.RemoveAt(e.RowIndex)
                        'GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                        'GridTransaksi.CurrentCell.Selected = True
                        GridTransaksi.CurrentCell = GridTransaksi.Item(0, e.RowIndex)
                        GridTransaksi.Item(0, e.RowIndex).Selected = True
                    End If
                End If
            End If
            For Each dgr As DataGridViewRow In GridTransaksi.Rows
                If dgr.Index <> GridTransaksi.CurrentCell.RowIndex Then
                    If dgr.Cells(0).Value = GridTransaksi.CurrentCell.Value Then
                        'dgr.Cells(3).Value = dgr.Cells(3).Value + 1
                        'dgr.Cells(5).Value = dgr.Cells(3).Value * dgr.Cells(2).Value

                        GridTransaksi.Rows.Remove(GridTransaksi.CurrentRow)
                      
                    End If
                End If

            Next
        ElseIf GridTransaksi.CurrentCell.ColumnIndex = 2 Then
            'If Not IsDBNull(GridTransaksi.CurrentCell) Then
            '    If Not GridTransaksi.CurrentCell.Value = "" Then
            '        GridTransaksi(5, e.RowIndex).Value = GridTransaksi(2, e.RowIndex).Value * GridTransaksi(3, e.RowIndex).Value

            '    End If
            'End If
        End If
    End Sub

    Public Overrides Sub SetReset()
        MyBase.SetReset()
        Me.Close()
    End Sub

    Private Sub TransaksiObat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GridTransaksi.Focus()
    End Sub
    Private txtnopo As String
    Private Sub getnotrx()
        txtnopo = ""
        Dim dt As New DataTable
        If Utils.GetFunction("getidtrx", New Object() {}, dt) Then
            If Utils.RowExist(dt) Then
                txtnopo = dt(0)(0)
            End If
        End If
    End Sub

End Class